package com.stars.easyms.logger.config;

import com.stars.easyms.base.util.ClassExistsUtil;
import com.stars.easyms.logger.support.tomcat.EasyMsTomcatEmbeddedLogFactoryHandler;
import com.stars.easyms.logger.util.SpringBootLoggingSystemUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * <p>className: EasyMsSpringBootLoggerListener</p>
 * <p>description: SpringBoot版本的监听器：开启异步Logging</p>
 *
 * @author guoguifang
 * @date 2019-12-27 14:05
 * @since 1.5.0
 */
public class EasyMsSpringBootLoggerListener implements SpringApplicationRunListener, Ordered {

    public EasyMsSpringBootLoggerListener(SpringApplication application, String[] args) {
        // Intentionally blank
    }

    @Override
    public void starting() {
        if (EasyMsLoggerInitializer.redefine()) {
            System.setProperty(LoggingSystem.SYSTEM_PROPERTY, SpringBootLoggingSystemUtil.getEasyMsLogbackLoggingSystemClassName());
            // 如果内嵌的tomcat存在的话将内嵌的tomcat的日志也改为异步的
            if (ClassExistsUtil.isExist("org.apache.juli.logging.LogFactory")) {
                EasyMsTomcatEmbeddedLogFactoryHandler.setDiscoveredLogConstructor();
            }
        }
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        // Intentionally blank
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        // Intentionally blank
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        // Intentionally blank
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        // Intentionally blank
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        // Intentionally blank
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        // Intentionally blank
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}